package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *课程大纲
 *
 * 课程章节
 * @author testjava
 * @since 2023-03-05
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /**
     * 课程大纲列表，需要根据id进行查询
     * @param courseId
     * @return
     */
    @GetMapping("/getChapter/{courseId}")
    public R getChapterByCourseId(@PathVariable  String courseId)
    {
        List list=chapterService.getChapterByCourseId(courseId);
        return R.ok().data("AllChapterVideo",list);
    }

    /**
     * 添加课程章节
     * @param eduChapter
     * @return
     */
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter)
    {
        boolean save = chapterService.save(eduChapter);
        if(!save)
        {
            return R.error().data("20001","添加失败");
        }
        return R.ok();

    }

    /**
     * 修改章节
     * @param eduChapter
     * @return
     */
    @PutMapping("/updateChapter")
    public R updateChapter(@RequestBody  EduChapter eduChapter) {

        chapterService.updateById(eduChapter);
        return R.ok();
    }


    /**
     * 根据章节id删除章节，章节下面的小节一起删除
     * @param chapterId
     * @return
     */
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId)
    {
        chapterService.deleteChapterAndVideo(chapterId);
        return R.ok();
    }

    /**
     * 根据章节id查询章节相关信息
     * @param
     * @return
     */
    @GetMapping("/getChapterByChapterId/{chapterId}")
    public R getChapterByChapterId(@PathVariable String chapterId)
    {

        EduChapter eduChapter= chapterService.getById(chapterId);
        System.out.println(eduChapter);
        return R.ok().data("eduChapter",eduChapter);
    }


}

